package avicit.bdp.dms.tdm.rest;

import avicit.bdp.dms.tdm.dto.AssetApplyDTO;
import avicit.bdp.dms.tdm.dto.BdpAssetDTO;
import avicit.bdp.dms.tdm.service.AssetApplyService;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：${tableInfo.classCreator}
 * @邮箱：${tableInfo.classMailbox}
 * @创建时间： 2023-11-14 16:03
 * @类说明：bdp_asset_applyRest
 * @修改记录：
 */
@RestController
@Api(tags = "数据资产申请")
@RequestMapping("/api/bdp/dms/asset/apply")
public class AssetApplyRest {

    private static final Logger logger = LoggerFactory.getLogger(AssetApplyRest.class);

    @Autowired
    private AssetApplyService assetApplyService;

    @ApiOperation(value = "分页查询资产申请")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "资产ID", name = "assetId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
    })
    @GetMapping("/getApplyPageList")
    public ResponseMsg<QueryRespBean<AssetApplyDTO>> getApplyPageList(
            @RequestParam String assetId,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        ResponseMsg<QueryRespBean<AssetApplyDTO>> responseMsg = new ResponseMsg<>();
        QueryRespBean<AssetApplyDTO> result = assetApplyService.getApplyPageList(assetId, pageNo, pageSize);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    @ApiOperation(value = "新增资产申请数据")
    @PostMapping("/saveByAssetId")
    public ResponseMsg<AssetApplyDTO> saveByAssetId(@RequestBody AssetApplyDTO dto) {
        ResponseMsg<AssetApplyDTO> responseMsg = new ResponseMsg<>();
        String id = assetApplyService.saveByAssetId(dto);
        if (StringUtils.isNotBlank(id)) {
            responseMsg.setResponseBody(assetApplyService.queryById(id));
        } else {
            throw new BusinessException("保存失败");
        }
        return responseMsg;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public ResponseMsg<AssetApplyDTO> save(@RequestBody AssetApplyDTO dto) {
        ResponseMsg<AssetApplyDTO> responseMsg = new ResponseMsg<>();
        String id = assetApplyService.save(dto);
        if (StringUtils.isNotBlank(id)) {
            responseMsg.setResponseBody(assetApplyService.queryById(dto.getId()));
        } else {
            throw new BusinessException("保存失败");
        }
        return responseMsg;
    }

    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public ResponseMsg<Boolean> update(@RequestBody AssetApplyDTO dto) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        assetApplyService.update(dto);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    @ApiOperation(value = "通过ID查询")
    @ApiParam(value = "id", name = "id", required = true)
    @GetMapping("/{id}")
    public ResponseMsg<AssetApplyDTO> queryById(@PathVariable String id) {
        ResponseMsg<AssetApplyDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetApplyService.queryById(id));
        return responseMsg;
    }

    @ApiOperation(value = "通过审核ID查询")
    @ApiParam(value = "approveId", name = "approveId", required = true)
    @GetMapping("/queryByApproveId")
    public ResponseMsg<AssetApplyDTO> queryByApproveId(@RequestParam String approveId) {
        ResponseMsg<AssetApplyDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetApplyService.queryByApproveId(approveId));
        return responseMsg;
    }

    @ApiOperation(value = "查询申请按钮")
    @ApiParam(value = "assetId", name = "assetId", required = true)
    @GetMapping("/queryApplyButton")
    public ResponseMsg<Map<String,String>> queryApplyButton(@RequestParam String assetId) {
        ResponseMsg<Map<String,String>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetApplyService.queryApplyButton(assetId));
        return responseMsg;
    }
}